package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @RequestMapping("/submit")
    public R<String> Submit(@RequestBody Orders orders){
        Long id = (Long) session.getAttribute("user");
        orders.setUserId(id);
        User byId = userService.findById(id);
        orders.setUserName(byId.getName());
        orders.setStatus(4);
        Boolean add = ordersService.add(orders);
        //付款后清空购物车
        shoppingCartService.deleteByIds(id);
        return R.success("订单完成");
    }

    @RequestMapping("/userPage")
    public R<Page<Orders>> findOrdersByPage(Integer page,Integer pageSize){
        Long id = (Long) session.getAttribute("user");
        Page<Orders> byPage = ordersService.findByPage(id, page, pageSize);
        System.out.println(byPage.getTotal());
        return R.success(byPage);
    }

    @RequestMapping("/again")
    public R<Orders> again(@RequestBody Orders orders){
        Orders again = ordersService.again(orders.getId());
        return R.success(again);
    }
}
